package com.zfy.aclservice.service.impl;

import com.zfy.aclservice.entity.User;
import com.zfy.aclservice.entity.UserRole;
import com.zfy.aclservice.mapper.UserMapper;
import com.zfy.aclservice.service.UserRoleService;
import com.zfy.aclservice.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-01-12
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserRoleService userRoleService;

    //根据用户名查询用户
    @Override
    public User selectByUsername(String username) {
        return baseMapper.selectOne(new QueryWrapper<User>().eq("username", username));
    }

    @Override
    @Transactional
    public void removeUser(String id) {
        this.removeById(id);
        //同时删除用户角色表的数据
        QueryWrapper<UserRole> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", id);
        userRoleService.remove(wrapper);
    }
}
